﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using CUREEntity;
using System.Web.Security;
using DBUtility;

/*************************************************************************************

     * 作   者： 张 颖
  
*************************************************************************************/
namespace CUREDataAccessLayers
{
    /// 用户信息
    /// <summary>
    /// 用户信息
    /// </summary>
    public class MasterDal
    {
        ///SqlConnection conn = new SqlConnection(connectionString))
        /// <summary>
        /// 获取人员信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns>返回集合</returns>
        public DataSet GetMasterData(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
           // IList<MasterEntity> list = null;
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            StringBuilder commandTextTotal = new StringBuilder();
            string master_id = string.Empty;
            string master_name = string.Empty;
            #region 处理数据
            if (where.ContainsKey("id") && where["id"] != null && where["id"].ToString() != "")
            {
                master_id = where["id"].ToString();
            }
            if (where.ContainsKey("name") && where["name"] != null && where["name"].ToString() != "")
            {
                master_name = where["name"].ToString();
            }
            #endregion
            commandText.Append("select * from (");
            commandText.Append("select rownum as rid, m.id as ID, m.name as NAME, m.userid as USERID, (case when sex = '1' then '男' else '女' end) as SEX,");
            commandText.Append("  d.dept_name as DEPT, m.email as EMAIL, m.tel as TEL, g.group_name as GROUP_NAME, s1.name as JOB, m.skilllevel as SKILLLEVEL, nvl2(m.skill_transport,m.skill_transport||'%','') as SKILL_TRANSPORT, nvl2(m.skill_packaging,m.skill_packaging||'%','') as SKILL_PACKAGING, nvl2(m.skill_shipments,m.skill_shipments||'%','') as SKILL_SHIPMENTS,");
            commandText.Append("  decode(m.isemail,0,'是','否') as ISEMAIL");
            commandText.Append("  from ");
            commandText.Append("   master m left join systemconfig s1 on s1.type ='JB' and s1.code = m.job left join department d on d.dept_id = m.dept left join mastergroup c left join groupmanager g on g.group_id=c.group_id on c.master_id = m.id");
            commandText.Append(" where 1 = 1 ");




            commandTextTotal.Append("select count(*) from ");
            commandTextTotal.Append("   master m left join systemconfig s1 on s1.type ='JB' and s1.code = m.job left join department d on d.dept_id = m.dept left join mastergroup c left join groupmanager g on g.group_id=c.group_id on c.master_id = m.id");
            commandTextTotal.Append(" where 1 = 1 ");

            if (master_id != null && master_id != "")
            {
                commandText.Append(" and m.userid='" + master_id.Trim() + "'");
                commandTextTotal.Append(" and m.userid='" + master_id.Trim() + "'");
            }
            if (master_name != null && master_name != "")
            {
                commandText.Append(" and m.name='" + master_name.Trim() + "'");
                commandTextTotal.Append(" and m.name='" + master_name.Trim() + "'");
            }
            commandText.Append(" and rownum<=" + pageIndex * pageSize + ") table_tmp ");
            commandText.Append(" where table_tmp.rid>" + (pageIndex - 1) * pageSize);
            try
            {
                totalRecordCount = Convert.ToInt32(DbHelperOra.QueryTable(commandTextTotal.ToString()).Rows[0][0].ToString());
                totalPageCount = (totalRecordCount + pageSize - 1) / pageSize;
                ds = DbHelperOra.Query(commandText.ToString());
                //list = this.DbHelperInstance.GetPagedData<MasterEntity>(pageIndex, pageSize, commandText, " ID ", out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        /// <summary>
        /// 获取登录人员信息
        /// </summary>
        /// <param name="where">字典参数</param>
        public DataTable GetUserInfoById(string ID)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            commandText.Append("select m.userid as USERID, m.name as NAME, (case when sex = '1' then '男' else '女' end) as SEX,");
            commandText.Append("      d.dept_name as DEPT, s1.name as JOB, m.email as EMAIL, m.tel as TEL ");
            commandText.Append("  from ");
            commandText.Append("   master m left join systemconfig s1 on s1.type ='JB' and s1.code = m.job left join department d on d.dept_id = m.dept left join mastergroup c left join groupmanager g on g.group_id=c.group_id on c.master_id = m.id");
            commandText.Append(" where m.id='" + ID.Trim() + "' ");
            try
            {
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary> 
        /// 添加用户信息
        /// </summary>
        /// <param id="id">id</param>
        /// <returns>返回受影响的行数</returns>
        public bool InsertMasterData(IDictionary<string, object> where)
        {


            bool result = false;
            StringBuilder commandText = new StringBuilder();
            //StringBuilder sql = new StringBuilder();
            string did = GetMaxId().ToString();
            string pwd = GetMD5Hash("123456").ToString();
            string userid = string.Empty;
            string name = string.Empty;
            string sex = string.Empty;
            string dept = string.Empty;
            string email = string.Empty;
            string group = string.Empty;
            string tel = string.Empty;
            string job = string.Empty;
            string level = string.Empty;
            string skill_transport = string.Empty;
            string skill_packaging = string.Empty;
            string skill_shipments = string.Empty;
            string isEMail = string.Empty;
            #region 数据处理
            if (where.ContainsKey("userid") && where["userid"] != null && where["userid"].ToString() != "")
            {
                userid = where["userid"].ToString().Trim();
            }
            if (where.ContainsKey("name") && where["name"] != null && where["name"].ToString() != "")
            {
                name = where["name"].ToString().Trim();
            }
            if (where.ContainsKey("sex") && where["sex"] != null && where["sex"].ToString() != "")
            {
                sex = where["sex"].ToString().Trim();
            }
            if (where.ContainsKey("dept") && where["dept"] != null && where["dept"].ToString() != "")
            {
                dept = where["dept"].ToString().Trim();
            }
            if (where.ContainsKey("email") && where["email"] != null && where["email"].ToString() != "")
            {
                email = where["email"].ToString().Trim();
            }
            if (where.ContainsKey("group") && where["group"] != null && where["group"].ToString() != "")
            {
                group = where["group"].ToString().Trim();
            }
            if (where.ContainsKey("tel") && where["tel"] != null && where["tel"].ToString() != "")
            {
                tel = where["tel"].ToString().Trim();
            }
            if (where.ContainsKey("job") && where["job"] != null && where["job"].ToString() != "")
            {
                job = where["job"].ToString().Trim();
            }
            if (where.ContainsKey("level") && where["level"] != null && where["level"].ToString() != "")
            {
                level = where["level"].ToString().Trim();
            }
            if (where.ContainsKey("skill_transport") && where["skill_transport"] != null && where["skill_transport"].ToString() != "")
            {
                skill_transport = int.Parse(where["skill_transport"].ToString().Trim()).ToString();
            }
            if (where.ContainsKey("skill_packaging") && where["skill_packaging"] != null && where["skill_packaging"].ToString() != "")
            {
                skill_packaging = int.Parse(where["skill_packaging"].ToString().Trim()).ToString();
            }
            if (where.ContainsKey("skill_shipments") && where["skill_shipments"] != null && where["skill_shipments"].ToString() != "")
            {
                skill_shipments = int.Parse(where["skill_shipments"].ToString().Trim()).ToString();
            }
            if (where.ContainsKey("isEMail") && where["isEMail"] != null && where["isEMail"].ToString() != "")
            {
                isEMail = int.Parse(where["isEMail"].ToString().Trim()).ToString();
            }
            #endregion

            bool judge = JudgeMasterIDExist(userid);
            bool judgeN = JudgeMasterNameExist(name);

            if ((judge == false) && (judgeN == false))
            {
                commandText.Append("begin");
                commandText.Append(" insert into master(id,userid,name,password,sex,dept,email,tel,create_date,job,skilllevel,skill_transport,skill_packaging,skill_shipments,isEMail)values('" + did + "','" + userid + "','" + name + "','" + pwd + "','" + sex + "','" + dept + "','" + email + "','" + tel + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + job + "','" + level + "','" + skill_transport + "','" + skill_packaging + "','" + skill_shipments + "','" + isEMail + "');");
                commandText.Append(" insert into mastergroup(master_id,group_id,datetime)values('" + did + "','" + group + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "');");
                commandText.Append(" commit;");
                commandText.Append(" rollback;");
                commandText.Append(" end;");
                //commandText.Append("insert into master(id,name,password,sex,dept,email,create_date)values('" + id + "','" + name + "','123456','" + sex + "','" + dept + "','" + email + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "')");
                //sql.Append("insert into mastergroup(master_id,group_id,datetime)values('" + id + "','" + group + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "')");

                try
                {
                    int i = 0;
                    i = DbHelperOra.ExecuteSql(commandText.ToString());
                    //i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, commandText);
                    //int j = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                    if (i != 0)
                    //if (i == 1 & j == 1)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return result;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回受影响的行数</returns>
        public bool UpdateMasterData(IDictionary<string, object> where)
        {
            bool result = false;
            StringBuilder commandText = new StringBuilder();

            int id = 0;
            string userid = string.Empty;
            string name = string.Empty;
            string sex = string.Empty;
            string dept = string.Empty;
            string email = string.Empty;
            string group = string.Empty;
            string tel = string.Empty;
            string job = string.Empty;
            string level = string.Empty;
            string skill_transport = string.Empty;
            string skill_packaging = string.Empty;
            string skill_shipments = string.Empty;
            string isEMail = string.Empty;
            #region 数据处理

            if (where.ContainsKey("id") && where["id"] != null && where["id"].ToString() != "")
            {
                id = int.Parse(where["id"].ToString().Trim());
            }
            if (where.ContainsKey("name") && where["name"] != null && where["name"].ToString() != "")
            {
                name = where["name"].ToString().Trim();
            }
            if (where.ContainsKey("sex") && where["sex"] != null && where["sex"].ToString() != "")
            {
                sex = where["sex"].ToString().Trim();
            }
            if (where.ContainsKey("dept") && where["dept"] != null && where["dept"].ToString() != "")
            {
                dept = where["dept"].ToString().Trim();
            }
            if (where.ContainsKey("email") && where["email"] != null && where["email"].ToString() != "")
            {
                email = where["email"].ToString().Trim();
            }
            if (where.ContainsKey("group") && where["group"] != null && where["group"].ToString() != "")
            {
                group = where["group"].ToString().Trim();
            }
            if (where.ContainsKey("tel") && where["tel"] != null && where["tel"].ToString() != "")
            {
                tel = where["tel"].ToString().Trim();
            }
            if (where.ContainsKey("job") && where["job"] != null && where["job"].ToString() != "")
            {
                job = where["job"].ToString().Trim();
            }
            if (where.ContainsKey("level") && where["level"] != null && where["level"].ToString() != "")
            {
                level = where["level"].ToString().Trim();
            }
            if (where.ContainsKey("skill_transport") && where["skill_transport"] != null && where["skill_transport"].ToString() != "")
            {
                skill_transport = int.Parse(where["skill_transport"].ToString().Trim()).ToString();
            }
            if (where.ContainsKey("skill_packaging") && where["skill_packaging"] != null && where["skill_packaging"].ToString() != "")
            {
                skill_packaging = int.Parse(where["skill_packaging"].ToString().Trim()).ToString();
            }
            if (where.ContainsKey("skill_shipments") && where["skill_shipments"] != null && where["skill_shipments"].ToString() != "")
            {
                skill_shipments = int.Parse(where["skill_shipments"].ToString().Trim()).ToString();
            }
            if (where.ContainsKey("isEMail") && where["isEMail"] != null && where["isEMail"].ToString() != "")
            {
                isEMail = int.Parse(where["isEMail"].ToString().Trim()).ToString();
            }           
            bool Judgegroup = CheckGroupId(id);
            #endregion

            commandText.Append("begin");
            commandText.Append(" update master set name='" + name
                + "',sex='" + sex
                + "',dept='" + dept
                + "',email='" + email
                + "',tel='" + tel
                + "',create_date='" + DateTime.Now.ToString("yyyy-MM-dd")
                + "',job='" + job
                + "',skilllevel='" + level
                + "',skill_transport='" + skill_transport
                + "',skill_packaging='" + skill_packaging
                + "',skill_shipments='" + skill_shipments
                + "',isEMail='" + isEMail
                + "' where id='" + id + "';");
            if (Judgegroup == true)
            {
                commandText.Append(" update mastergroup set group_id=" + int.Parse(group.Trim())
                        + ",datetime='" + DateTime.Now.ToString("yyyy-MM-dd")
                        + " ' where master_id=" + id + ";");
            }
            else
            {
                commandText.Append("insert into mastergroup(master_id,group_id,datetime)values(" + id + "," + int.Parse(group) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "');");
            }
            commandText.Append(" commit;");
            commandText.Append(" rollback;");
            commandText.Append(" end;");


            try
            {
                int i = DbHelperOra.ExecuteSql(commandText.ToString());
                if (i > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 判定group中是否已有该用户信息
        /// </summary>
        /// <param name="master_id"></param>
        /// <returns></returns>
        public bool CheckGroupId(int master_id)
        {
            bool resut = false;
            int group_id = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append("select count(m.group_id) from mastergroup m where m.master_id=" + master_id + "");
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    group_id = Convert.ToInt32(obj);
                }
                //group_id = Convert.ToInt32(this.DbHelperInstance.ExecuteScalar<decimal>(sql));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (group_id != 0)
            {
                resut = true;
            }
            return resut;
        }

        /// <summary>
        /// 根据id删除用户信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回表</returns>
        public bool DeleteMasterData(string did)
        {
            bool result = false;
            StringBuilder commandText = new StringBuilder();
            //StringBuilder sql = new StringBuilder();

            commandText.Append("begin");
            commandText.Append(" delete from master where id='" + did + "';");
            commandText.Append(" delete from mastergroup where master_id='" + did + "';");
            commandText.Append(" commit;");
            commandText.Append(" rollback;");
            commandText.Append(" end;");

            try
            {
                //this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                DbHelperOra.ExecuteSql(commandText.ToString());
                //this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, commandText);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据id重置密码
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回表</returns>
        public bool RestoreMasterData(string did)
        {
            bool result = false;
            StringBuilder commandText = new StringBuilder();
            string pwd = GetMD5Hash("123456");

            commandText.Append("update master set password ='" + pwd + "' where id='" + did + "'");
            try
            {
                DbHelperOra.ExecuteSql(commandText.ToString());
                //this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, commandText);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回表</returns>
        public DataTable GetMasterById(string did)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select m.id, m.userid, m.name, m.sex, g.group_id, g.group_name, m.email, m.tel, m.job, m.skilllevel, m.skill_transport, m.skill_packaging, m.skill_shipments, d.dept_id, d.dept_name, m.isEmail");
                commandText.Append("  from master m left join mastergroup c left join groupmanager g on g.group_id = c.group_id on m.id = c.master_id left join department d on d.dept_id=m.dept");
                commandText.Append(" where 1=1");
                //commandText.Append("       m.sex,");
                //commandText.Append("       g.group_id,");
                //commandText.Append("       g.group_name,");
                //commandText.Append("       m.email,m.tel,m.job,m.skilllevel,m.skill_transport,m.skill_packaging,m.skill_shipments,");
                //commandText.Append("       d.dept_id,");
                //commandText.Append("       d.dept_name");
                //commandText.Append(" from master m,groupmanager g,mastergroup c,department d");
                //commandText.Append(" where m.id=c.master_id and g.group_id=c.group_id and TRIM(m.dept)=TRIM(d.dept_id(+)) ");
                if (!string.IsNullOrEmpty(did))
                {
                    commandText.Append(" and m.id=" + did.Trim() + "");
                }
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetGroup()
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select g.group_id,g.group_name from groupmanager g");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetDept()
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select d.dept_id,d.dept_name from department d");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取职能信息
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetJob()
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select s.code, s.name from systemconfig s where s.type='JB'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取等级信息
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetSkilllevel()
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select s.code from systemconfig s where s.type='LV'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetMaster(string userName)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select t.id,t.name from master t where 1=1");
                if (!string.IsNullOrEmpty(userName))
                {
                    commandText.Append(" and name='" + userName.Trim() + "'");
                }
                dt = DbHelperOra.QueryTable(commandText.ToString());
               // this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 根据用户名称获取用户ID
        /// </summary>
        /// <param name="userName">用户名称</param>
        /// <returns>用户ID</returns>
        /// zcf  2014年10月27日13
        public decimal GetMasterIDbyName(string userName)
        {
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select t.id,t.name from master t where 1=1");
                if (!string.IsNullOrEmpty(userName))
                {
                    commandText.Append(" and name='" + userName.Trim() + "'");
                }
                decimal id = 0;
                Object obj = DbHelperOra.GetSingle(commandText.ToString());
                if (!Object.Equals(obj, null))
                {
                    id = Convert.ToDecimal(obj);
                }
                return id;
                //return this.DbHelperInstance.ExecuteScalar<decimal>(commandText);
            }
            catch (Exception ex)
            {
                return -2;
            }
        }

        /// <summary>
        /// 判断员工编号是否存在
        /// </summary>
        /// <param name="dept_id">编号</param>
        /// <returns>true 存在/fasle 不存在</returns>
        public bool JudgeMasterIDExist(string id)
        {
            bool resut = false;
            string masterid = string.Empty;
            StringBuilder sql = new StringBuilder();
            sql.Append("select m.userid from master m where m.userid='" + id + "'");
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    masterid = obj.ToString();
                }
                //masterid = this.DbHelperInstance.ExecuteScalar<string>(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (masterid != null && masterid != "")
            {
                resut = true;
            }
            return resut;
        }
        /// <summary>
        /// 判断员工姓名是否存在
        /// </summary>
        /// <param name="dept_id">姓名</param>
        /// <returns>true 存在/fasle 不存在</returns>
        public bool JudgeMasterNameExist(string name)
        {
            bool resut = false;
            string masterid = string.Empty;
            StringBuilder sql = new StringBuilder();
            sql.Append("select m.userid from master m where m.name='" + name + "'");
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    masterid = obj.ToString();
                }
                //masterid = this.DbHelperInstance.ExecuteScalar<string>(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (masterid != null && masterid != "")
            {
                resut = true;
            }
            return resut;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="where">userid:用户编号，password:新密码</param>
        /// <returns></returns>
        public bool UpdateMasterPassword(IDictionary<string, object> where)
        {
            bool result = false;
            string userid = string.Empty;
            string password = string.Empty;
            StringBuilder sql = new StringBuilder();
            #region 处理参数
            if (where.ContainsKey("userid") && where["userid"] != null && where["userid"].ToString() != "")
            {
                userid = where["userid"].ToString();
            }
            if (where.ContainsKey("password") && where["password"] != null && where["password"].ToString() != "")
            {
                password = where["password"].ToString();
            }
            #endregion
            sql.Append("update master set password='" + password + "' where id='" + userid + "'");
            try
            {
                int i = DbHelperOra.ExecuteSql(sql.ToString());
                if (i > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns>返回最大的id+1</returns>
        public int GetMaxId()
        {
            int maxid = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(to_number(id))+1 from master m");
                maxid = DbHelperOra.GetMaxID("id", "master");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxid;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Password">密码明文</param>
        /// <returns>返回16位密文</returns>
        /// Remark：修改处理MD5加密
        public static string GetMD5Hash(String Password)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5").ToUpper().Substring(8, 16);
        }

        /// <summary>
        /// 判断原始密码是否正确
        /// </summary>
        /// <param name="where">userid:用户编号，oldpassword:原始密码</param>
        /// <returns>true：正确，false：错误</returns>
        public bool CheckMasterPassword(IDictionary<string, object> where)
        {
            bool result = false;
            string userid = string.Empty;
            string oldpassword = string.Empty;
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            #region 处理参数
            if (where.ContainsKey("userid") && where["userid"] != null && where["userid"].ToString() != "")
            {
                userid = where["userid"].ToString();
            }
            if (where.ContainsKey("oldpassword") && where["oldpassword"] != null && where["oldpassword"].ToString() != "")
            {
                oldpassword = where["oldpassword"].ToString();
            }
            #endregion
            sql.Append(" select * from master where id='" + userid + "' and password='" + oldpassword + "'");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
                //this.DbHelperInstance.Fill(dt, sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 打印员工编号
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable SelectprintUserNum(IDictionary<string, object> where)
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            try
            {
                sql.Append("select  name,userid  from  master where 1=1");
                if (where["usernum"] != null && where["usernum"].ToString() != "")
                {
                    sql.Append(" and  userid='" + where["usernum"].ToString() + "' ");
                }
                sql.Append(" order by userid");
                dt = DbHelperOra.QueryTable(sql.ToString());
                //this.DbHelperInstance.Fill(dt, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
